package com.lipiao.smarthome.fragments;

import android.content.DialogInterface;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TimePicker;
import android.widget.Toast;

import com.lipiao.smarthome.Dialog.SingleChoiceDialogFragment;
import com.lipiao.smarthome.MainActivity;
import com.lipiao.smarthome.R;
import com.lipiao.smarthome.TimeLine.DotTimeLineAdapter;
import com.lipiao.smarthome.TimeLine.Event;
import com.lipiao.smarthome.TimePick.NewDateDialog;
import com.lipiao.smarthome.TimePick.PickerView;
import com.lipiao.smarthome.TimePick.ViewDialogFragment;
import com.vivian.timelineitemdecoration.itemdecoration.DotItemDecoration;
import com.vivian.timelineitemdecoration.itemdecoration.SpanIndexListener;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

public class SetFragment extends Fragment {
    View rootView;

    Button ok;
    Button changeModelBut;
    private int index;//模式选择 下标

//    //time pick
//    PickerView minute_pv;
//    PickerView second_pv;

    //timeline
    RecyclerView mRecyclerView;
    List<Event> mList = new ArrayList<>();
    DotTimeLineAdapter mAdapter;
    DotItemDecoration mItemDecoration;

    String[] items = {"回家模式", "娱乐模式", "离开模式"};

    String[] times = {
            "7:00",
            "7:30",
            "8:00",
            "9:00",
            "11:00",
            "12:00",
            "14:00",
            "16:30",
            "17:30",
            "18:00",
            "21:30"
    };
    String[] events = new String[]{
            "开灯",
            "窗帘打开",
            "唤醒智能音箱",
            "启动扫地机器人",
            "打开饮水机",
            "关闭扫地机器人",
            "关闭智能音箱",
            "打开空调",
            "窗帘关闭",
            "关灯",
            "关闭空调",
    };


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment_set, container, false);
        initView();
        initListener();
//        showViewDialogFragment(rootView);
        return rootView;
    }

    private void initView() {

        ok=rootView.findViewById(R.id.ok_bt);
        changeModelBut=rootView.findViewById(R.id.model_bt);
        //
////        minute_pv = rootView.findViewById(R.id.minute_pv);
////        second_pv = rootView.findViewById(R.id.second_pv);
//        List<String> data = new ArrayList<String>();
//        List<String> seconds = new ArrayList<String>();
//        for (int i = 0; i < 24; i++) {
//            data.add(""+i);
//        }
//        for (int i = 0; i < 60; i++) {
//            seconds.add(i < 10 ? "0" + i : "" + i);
//        }
//        minute_pv.setData(data);
//        minute_pv.setOnSelectListener(new PickerView.onSelectListener() {
//
//            @Override
//            public void onSelect(String text) {
//                //hour
//            }
//        });
//        second_pv.setData(seconds);
//        second_pv.setOnSelectListener(new PickerView.onSelectListener() {
//
//            @Override
//            public void onSelect(String text) {
//                //minute
//            }
//        });

        //
        mRecyclerView = rootView.findViewById(R.id.recycler_view);
        mRecyclerView.setLayoutManager(new

                StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));
        mItemDecoration = new DotItemDecoration
                .Builder(
                getContext())
                .setOrientation(DotItemDecoration.VERTICAL)//if you want a horizontal item decoration,remember to set horizontal orientation to your LayoutManager
                .setItemStyle(DotItemDecoration.STYLE_DRAW)
                .setTopDistance(20)//dp
                .setItemInterVal(10)//dp
                .setItemPaddingLeft(20)//default value equals to item interval value
                .setItemPaddingRight(20)//default value equals to item interval value
                .setDotColor(Color.WHITE)
                .setDotRadius(5)//dp
                .setDotPaddingTop(0)
                .setDotInItemOrientationCenter(false)//set true if you want the dot align center
                .setLineColor(Color.BLACK)
                .setLineWidth(2)//dp
                .setEndText("END")
                .setTextColor(Color.WHITE)
                .setTextSize(10)//sp
                .setDotPaddingText(2)//dp.The distance between the last dot and the end text
                .setBottomDistance(40)//you can add a distance to make bottom line longer
                .create();
        mItemDecoration.setSpanIndexListener(new SpanIndexListener() {
            @Override
            public void onSpanIndexChange(View view, int spanIndex) {
                Log.i("Info", "view:" + view + "  span:" + spanIndex);
                view.setBackgroundResource(spanIndex == 0 ? R.drawable.pop_left : R.drawable.pop_right);
            }
        });
        mRecyclerView.addItemDecoration(mItemDecoration);

        for (
                int i = 0;
                i < times.length; i++)

        {
            Event event = new Event();
            event.setTime(times[i]);
            event.setEvent(events[i]);
            mList.add(event);
        }

        mAdapter = new

                DotTimeLineAdapter(getContext(), mList);
        mRecyclerView.setAdapter(mAdapter);
    }//initveiw

    private void initListener() {

        ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Calendar currentTime = Calendar.getInstance();
                // 弹出一个时间设置的对话框,供用户选择时间
                new NewDateDialog(getContext(), 0,
                        new NewDateDialog.OnTimeSetListener() {
                            @Override
                            public void onTimeSet(TimePicker view,
                                                  int hourOfDay, int minute) {


                                // 提示闹钟设置完毕:
                                Toast.makeText(getContext(), "设置完毕"+hourOfDay+":"+minute,
                                        Toast.LENGTH_SHORT).show();
                            }
                        }, currentTime.get(Calendar.HOUR_OF_DAY), currentTime
                        .get(Calendar.MINUTE), true).show();
            }
        });

        changeModelBut.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showSingleChoiceDialogFragment(view);
            }
        });

    }
    public void showSingleChoiceDialogFragment(View view) {
        SingleChoiceDialogFragment singleChoiceDialogFragment = new SingleChoiceDialogFragment();
        singleChoiceDialogFragment.show("模式选择", items, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                index = which;
            }
        }, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(getActivity(), "选择了" + items[index + 1] , Toast.LENGTH_SHORT).show();
            }
        }, getFragmentManager());
    }

//    //弹窗
//    public void showViewDialogFragment(View view) {
//        ViewDialogFragment viewDialogFragment = new ViewDialogFragment();
//        viewDialogFragment.show(getFragmentManager());
//    }

}
